Skip to content

Conversation

@NicolasMassart
Copy link
Contributor

@NicolasMassart NicolasMassart commented Jan 15, 2026

Explanation

This PR introduces a new @metamask/analytics-privacy-controller package that provides GDPR/CCPA data deletion functionality for analytics data. The package allows to extract the logic from the mobile app (and will be compatible with extension too)

Current state: MetaMask mobile app currently has a dedicated mechanism to handle user data deletion requests for analytics data in compliance with GDPR and CCPA regulations.

Solution: This package introduces:

  • AnalyticsPrivacyController: A controller that manages the lifecycle of data deletion requests, tracks whether new data has been recorded since the last deletion request, and stores deletion regulation metadata (ID and timestamp)
  • AnalyticsPrivacyService: A service that communicates with Segment's Regulations API via a proxy endpoint to create deletion tasks and check their status
  • State management: Tracks dataRecorded flag, deleteRegulationId, and deleteRegulationTimestamp to support compliance workflows
  • Selectors: Provides reusable selectors for accessing controller state

Implementation details:

  • The controller calls AnalyticsController:getState to retrieve the user's analytics ID
  • It delegates to AnalyticsPrivacyService to make HTTP requests to Segment's Regulations API
  • The service uses createServicePolicy from @metamask/controller-utils for retry logic and error handling
  • State is persisted and can be used to determine if new analytics events have been recorded since the last deletion request
  • The package includes comprehensive test coverage (100% branch, function, line, and statement coverage)

References

see also MetaMask/metamask-mobile#22016

Fixes #7618

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Adds a new package @metamask/analytics-privacy-controller for analytics GDPR/CCPA data deletion workflows.

  • Introduces AnalyticsPrivacyController (state: dataRecorded, deleteRegulationId, deleteRegulationTimestamp; methods: createDataDeletionTask, checkDataDeleteStatus, getters, and flag update; events for task creation/state changes)
  • Adds AnalyticsPrivacyService (proxy HTTP calls to Segment Regulations API; retry/circuit-breaker via createServicePolicy; exposed actions for create/check)
  • Provides selectors, constants, types, logging, TypeScript/Jest configs, and 100% coverage tests for controller/service
  • Wires package into repo (README, CODEOWNERS, teams.json, tsconfig references, yarn.lock)

Written by Cursor Bugbot for commit 2664bfb. This will update automatically on new commits. Configure here.

NicolasMassart and others added 6 commits January 15, 2026 14:21
…ad of dates

- Changed `deleteRegulationDate` to `deleteRegulationTimestamp` in the state and related methods to store timestamps in milliseconds since epoch.
- Updated relevant methods and tests to reflect the new timestamp format.
- Removed date formatting logic and adjusted selectors accordingly.
- Added new dependencies for testing and updated the test suite to ensure proper functionality with the new timestamp format.

This change enhances consistency in handling date-related data within the analytics privacy controller.
@socket-security
Copy link

socket-security bot commented Jan 15, 2026

No dependency changes detected. Learn more about Socket for GitHub.

👍 No dependency changes detected in pull request

@NicolasMassart NicolasMassart marked this pull request as draft January 15, 2026 18:17
NicolasMassart and others added 3 commits January 16, 2026 11:06
…e PascalCase

- Refactored enum values in `DataDeleteResponseStatus` and `DataDeleteStatus` to follow PascalCase naming convention.
- Updated all references in the codebase and tests to ensure consistency with the new enum values.
- This change enhances code readability and aligns with common TypeScript practices.
@NicolasMassart NicolasMassart marked this pull request as ready for review January 16, 2026 14:32
@NicolasMassart NicolasMassart requested a review from a team as a code owner January 16, 2026 14:32
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new @metamask/analytics-privacy-controller package that provides GDPR/CCPA data deletion functionality for analytics data, extracting logic from the mobile app for reuse across MetaMask clients.

Changes:

  • New controller package with AnalyticsPrivacyController for managing data deletion state (tracks dataRecorded, deleteRegulationId, and deleteRegulationTimestamp)
  • AnalyticsPrivacyService for communicating with Segment's Regulations API via proxy endpoint with retry/circuit-breaker logic
  • Comprehensive test coverage (100% branch, function, line, and statement coverage) and selector utilities for state access

Reviewed changes

Copilot reviewed 24 out of 26 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/analytics-privacy-controller/src/AnalyticsPrivacyController.ts Main controller implementation managing deletion request lifecycle and state
packages/analytics-privacy-controller/src/AnalyticsPrivacyService.ts Service for HTTP communication with Segment Regulations API via proxy
packages/analytics-privacy-controller/src/types.ts Type definitions for deletion statuses and API responses
packages/analytics-privacy-controller/src/selectors.ts Reusable state selectors
packages/analytics-privacy-controller/src/constants.ts Segment API constants
packages/analytics-privacy-controller/package.json Package configuration with dependencies
packages/analytics-privacy-controller/*.test.ts Comprehensive test suites for controller and service
yarn.lock Lockfile updates for new package dependencies
tsconfig.json, tsconfig.build.json TypeScript project references
teams.json, .github/CODEOWNERS Team ownership configuration
README.md Documentation updates including package in list and dependency graph

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@NicolasMassart
Copy link
Contributor Author

@metamaskbot publish-preview

@github-actions
Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/account-tree-controller": "4.0.0-preview-fa81dffb",
  "@metamask-previews/accounts-controller": "35.0.2-preview-fa81dffb",
  "@metamask-previews/address-book-controller": "7.0.1-preview-fa81dffb",
  "@metamask-previews/analytics-controller": "1.0.0-preview-fa81dffb",
  "@metamask-previews/analytics-privacy-controller": "0.0.0-preview-fa81dffb",
  "@metamask-previews/announcement-controller": "8.0.0-preview-fa81dffb",
  "@metamask-previews/app-metadata-controller": "2.0.0-preview-fa81dffb",
  "@metamask-previews/approval-controller": "8.0.0-preview-fa81dffb",
  "@metamask-previews/assets-controller": "0.0.0-preview-fa81dffb",
  "@metamask-previews/assets-controllers": "95.3.0-preview-fa81dffb",
  "@metamask-previews/base-controller": "9.0.0-preview-fa81dffb",
  "@metamask-previews/bridge-controller": "64.5.1-preview-fa81dffb",
  "@metamask-previews/bridge-status-controller": "64.4.3-preview-fa81dffb",
  "@metamask-previews/build-utils": "3.0.4-preview-fa81dffb",
  "@metamask-previews/chain-agnostic-permission": "1.4.0-preview-fa81dffb",
  "@metamask-previews/claims-controller": "0.4.1-preview-fa81dffb",
  "@metamask-previews/composable-controller": "12.0.0-preview-fa81dffb",
  "@metamask-previews/connectivity-controller": "0.1.0-preview-fa81dffb",
  "@metamask-previews/controller-utils": "11.18.0-preview-fa81dffb",
  "@metamask-previews/core-backend": "5.0.0-preview-fa81dffb",
  "@metamask-previews/delegation-controller": "2.0.0-preview-fa81dffb",
  "@metamask-previews/earn-controller": "11.1.0-preview-fa81dffb",
  "@metamask-previews/eip-5792-middleware": "2.1.0-preview-fa81dffb",
  "@metamask-previews/eip-7702-internal-rpc-middleware": "0.1.0-preview-fa81dffb",
  "@metamask-previews/eip1193-permission-middleware": "1.0.3-preview-fa81dffb",
  "@metamask-previews/ens-controller": "19.0.2-preview-fa81dffb",
  "@metamask-previews/error-reporting-service": "3.0.1-preview-fa81dffb",
  "@metamask-previews/eth-block-tracker": "15.0.1-preview-fa81dffb",
  "@metamask-previews/eth-json-rpc-middleware": "23.0.0-preview-fa81dffb",
  "@metamask-previews/eth-json-rpc-provider": "6.0.0-preview-fa81dffb",
  "@metamask-previews/foundryup": "1.0.1-preview-fa81dffb",
  "@metamask-previews/gas-fee-controller": "26.0.2-preview-fa81dffb",
  "@metamask-previews/gator-permissions-controller": "1.0.0-preview-fa81dffb",
  "@metamask-previews/json-rpc-engine": "10.2.1-preview-fa81dffb",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.8-preview-fa81dffb",
  "@metamask-previews/keyring-controller": "25.0.0-preview-fa81dffb",
  "@metamask-previews/logging-controller": "7.0.1-preview-fa81dffb",
  "@metamask-previews/message-manager": "14.1.0-preview-fa81dffb",
  "@metamask-previews/messenger": "0.3.0-preview-fa81dffb",
  "@metamask-previews/multichain-account-service": "5.0.0-preview-fa81dffb",
  "@metamask-previews/multichain-api-middleware": "1.2.6-preview-fa81dffb",
  "@metamask-previews/multichain-network-controller": "3.0.2-preview-fa81dffb",
  "@metamask-previews/multichain-transactions-controller": "7.0.0-preview-fa81dffb",
  "@metamask-previews/name-controller": "9.0.0-preview-fa81dffb",
  "@metamask-previews/network-controller": "29.0.0-preview-fa81dffb",
  "@metamask-previews/network-enablement-controller": "4.1.0-preview-fa81dffb",
  "@metamask-previews/notification-services-controller": "21.0.0-preview-fa81dffb",
  "@metamask-previews/permission-controller": "12.2.0-preview-fa81dffb",
  "@metamask-previews/permission-log-controller": "5.0.0-preview-fa81dffb",
  "@metamask-previews/phishing-controller": "16.1.0-preview-fa81dffb",
  "@metamask-previews/polling-controller": "16.0.2-preview-fa81dffb",
  "@metamask-previews/preferences-controller": "22.0.0-preview-fa81dffb",
  "@metamask-previews/profile-metrics-controller": "2.0.0-preview-fa81dffb",
  "@metamask-previews/profile-sync-controller": "27.0.0-preview-fa81dffb",
  "@metamask-previews/ramps-controller": "3.0.0-preview-fa81dffb",
  "@metamask-previews/rate-limit-controller": "7.0.0-preview-fa81dffb",
  "@metamask-previews/remote-feature-flag-controller": "4.0.0-preview-fa81dffb",
  "@metamask-previews/sample-controllers": "4.0.2-preview-fa81dffb",
  "@metamask-previews/seedless-onboarding-controller": "7.1.0-preview-fa81dffb",
  "@metamask-previews/selected-network-controller": "26.0.2-preview-fa81dffb",
  "@metamask-previews/shield-controller": "5.0.0-preview-fa81dffb",
  "@metamask-previews/signature-controller": "39.0.1-preview-fa81dffb",
  "@metamask-previews/storage-service": "0.0.1-preview-fa81dffb",
  "@metamask-previews/subscription-controller": "5.4.0-preview-fa81dffb",
  "@metamask-previews/token-search-discovery-controller": "4.0.0-preview-fa81dffb",
  "@metamask-previews/transaction-controller": "62.9.2-preview-fa81dffb",
  "@metamask-previews/transaction-pay-controller": "11.0.2-preview-fa81dffb",
  "@metamask-previews/user-operation-controller": "41.0.2-preview-fa81dffb"
}

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @NicolasMassart, sorry for the late review, I had some questions/comments below.

this.#messenger = options.messenger;
this.#fetch = options.fetch;
this.#segmentSourceId = options.segmentSourceId;
this.#segmentRegulationsEndpoint = options.segmentRegulationsEndpoint;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought around data services is that they should represent an API, and the implication is that the exact API they represent should be obvious. However, it seems that this class accepts any URL. That means the knowledge about which external system this is talking to is somewhere else.

Is it possible to include the URL in this same file, perhaps as a constant? If it's based on environment, perhaps the class can take an environment and use that to build the URL?

Copy link
Contributor

@mcmire mcmire Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since there is no class here, is there a reason why this file needs to be PascalCase? Thoughts on calling this file logger.ts?

/**
* Indicates if data has been recorded since the last deletion request.
*/
dataRecorded: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In checkDataDeleteStatus, this is renamed to hasCollectedDataSinceDeletionRequest. I think this name does a good job of describing the purpose. Thoughts on renaming the state property?

Suggested change
dataRecorded: boolean;
hasCollectedDataSinceDeletionRequest: boolean;

const { analyticsId } = analyticsControllerState;

if (!analyticsId || analyticsId.trim() === '') {
const error = new Error('Analytics ID not found');
Copy link
Contributor

@mcmire mcmire Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Do we want a more descriptive message which instructs the engineer on how to fix this? Perhaps something like "You need to set up AnalyticsController with an analytics ID. You can do this by..." (etc.)


if (
response.status === DataDeleteResponseStatus.Success &&
response.regulateId &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to be validating the response data. Is there a reason why we are doing that here? Ideally, the service should return data that is usable, and if not, should throw an error.

* Status values for data deletion requests from Segment API.
* Enum values match Segment API response values exactly.
*/
export enum DataDeleteStatus {
Copy link
Contributor

@mcmire mcmire Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on using a type object rather than an enum? We are considering banning enums in core (and other places) because they have a lot of issues, one of which is that two enums with the same name and same content aren't assignable to each other (nominal typing), which causes unexpected type errors. You can read more here: MetaMask/eslint-config#417

For instance, you could replace this enum with:

export enum DATA_DELETE_STATUSES = {
  Failed: 'FAILED',
  Finished: 'FINISHED',
  Initialized: 'INITIALIZED',
  Invalid: 'INVALID',
  NotSupported: 'NOT_SUPPORTED',
  PartialSuccess: 'PARTIAL_SUCCESS',
  Running: 'RUNNING',
  Unknown: 'UNKNOWN',
} as const;

export type DataDeleteStatus =
  (typeof DataDeleteStatus)[keyof typeof DataDeleteStatus];

/**
* Regulation ID from Segment API.
*/
export type DataDeleteRegulationId = string | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can an ID be undefined? That seems interesting 🤔

} catch (error) {
log('Analytics Deletion Task Error', error);
return {
status: DataDeleteResponseStatus.Failure,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question I posed in the controller — is there a reason why throwing an error would not achieve the same thing? Or in this case, since the service is supposed to be lower-level, is there a reason why it's useful to catch errors at all? Why not let them bubble up?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: These constants seem to only be used in the service. Would it be easier to maintain if they lived there?

Comment on lines +376 to +380
const dataDeleteStatus = Object.values(DataDeleteStatus).includes(
rawStatus as DataDeleteStatus,
)
? (rawStatus as DataDeleteStatus)
: DataDeleteStatus.Unknown;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: If you had an isDataDeleteStatus function then you could simplify this and wouldn't need to use typecasting:

function isDataDeleteStatus(status: unknown): status is DataDeleteStatus {
  const dataDeleteStatuses: string[] = Object.values(DataDeleteStatus);
  return dataDeleteStatuses.includes(status);
}
Suggested change
const dataDeleteStatus = Object.values(DataDeleteStatus).includes(
rawStatus as DataDeleteStatus,
)
? (rawStatus as DataDeleteStatus)
: DataDeleteStatus.Unknown;
const dataDeleteStatus = isDataDeleteStatus(rawStatus)
? rawStatus
: DataDeleteStatus.Unknown;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Needs dev review

Development

Successfully merging this pull request may close these issues.

feat: create analytics data deletion controller

3 participants